package com.hm.ibsm.web;

import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hm.common.util.DateUtil;
import com.hm.common.util.RandomUtil;
import com.hm.common.util.RemoteClientUtil;

/**
 * @author shishun.wang
 * @date 2017年3月30日 下午6:09:47
 * @version 1.0
 * @describe
 */
@Controller
public class LoginController {

	private static String SESSION_USER_NAME = "SESSION_USER_NAME";

	@RequestMapping(value = { "", "/", "/index.do" })
	public String index(ModelMap model) {
		model.put("serverIp", info());
		model.put("currentUserName", RandomUtil.userName());
		return "index";
	}

	@RequestMapping(value = "/session.do")
	public @ResponseBody String session(@RequestParam(name = "name", required = true) String name, HttpServletRequest request) {
		request.getSession().setAttribute(SESSION_USER_NAME, name);
		return info() + ",设置成功会话中用户名称是: <span style='color:green'>" + name + "</span>";
	}

	@RequestMapping(value = "/getUser.do")
	public @ResponseBody String getSessionUserName(HttpServletRequest request) {
		return info() + "会话中用户名称是:<span style='color:red'>" + request.getSession().getAttribute(SESSION_USER_NAME) + "</span>";
	}

	public String info() {
		String ip = null;
		try {
			ip = RemoteClientUtil.getLocalIpAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return "操作时间:[<span style='color:blue'>" + DateUtil.now4yyyyMMddhhmm() + "</span>]当前处理数据服务器ip地址是:[<span style='color:blue'>" + ip + "</span>]";
	}
}
